# code to produce 1/2, 1/4 and 1/8 size id-images

def doit():
    for i in range(1,6):
        pfile = 'e'+str(i)+'.jpg'
        p = makePicture(pfile)
        p2 = half(p)
        savePicture(p2,'e'+str(i)+'hlf.jpg')
        p4 = half(p2)
        savePicture(p4,'e'+str(i)+'qtr.jpg')
        p8 = half(p4)
        savePicture(p8,'e'+str(i)+'egt.jpg')
    for i in range(1,6):
        pfile = 'x'+str(i)+'.jpg'
        p = makePicture(pfile)
        p2 = half(p)
        savePicture(p2,'x'+str(i)+'hlf.jpg')
        p4 = half(p2)
        savePicture(p4,'x'+str(i)+'qtr.jpg')
        p8 = half(p4)
        savePicture(p8,'x'+str(i)+'egt.jpg')
    for i in range(1,43):
        pfile = 'id'+str(i)+'.jpg'
        p = makePicture(pfile)
        p2 = half(p)
        savePicture(p2,'id'+str(i)+'hlf.jpg')
        p4 = half(p2)
        savePicture(p4,'id'+str(i)+'qtr.jpg')
        p8 = half(p4)
        savePicture(p8,'id'+str(i)+'egt.jpg')
    pfile = 'home.jpg'
    p = makePicture(pfile)
    p2 = half(p)
    savePicture(p2,'homehf.jpg')
    p4 = half(p2)
    savePicture(p4,'homeqtr.jpg')
    p8 = half(p4)
    savePicture(p8,'homeegt.jpg')
    return 'Done!'

def half(p):
    w = 320
    h = 240
    newP = makePicture("black.jpg")
    plusi = w/4
    plusj = h/4
    for i in range(w/2):
        for j in range(h/2):
            r = 0
            g = 0
            b = 0
            pix = getPixel(p,2*i,2*j)
            ar,ag,ab = getRGB(pix)
            r += ar
            g += ag
            b += ab
            pix = getPixel(p,2*i+1,2*j)
            ar,ag,ab = getRGB(pix)
            r += ar
            g += ag
            b += ab
            pix = getPixel(p,2*i,2*j+1)
            ar,ag,ab = getRGB(pix)
            r += ar
            g += ag
            b += ab
            pix = getPixel(p,2*i+1,2*j+1)
            ar,ag,ab = getRGB(pix)
            r += ar
            g += ag
            b += ab
            nur = round(r/4.0)
            nug = round(g/4.0)
            nub = round(b/4.0)
            nupix = getPixel(newP,i+plusi,j+plusj)
            setRGB(nupix,(nur,nug,nub))
    return newP
